import Love from "../love/love"
import db from "../data/car"
import playerData from "../data/player"
var love = new Love("garage")

love.setCarData = function () {
  let i = love.index
  let cc = playerData.slot[i]
  let data = db[cc.type]
  let ui = love.uiData
  let up = cc.upgrade
  ui[1].obj.text = playerData.money + " ￥"
  ui[2].obj.image = love.loader.data.sheet.extend(data.quad)
  ui[4].obj.text = data.name
  ui[6].obj.text = data.type
  ui[7].data = data.enginepower
  ui[8].data = data.structure
  ui[9].data = data.streamline
  ui[10].data = data.fueltank
  ui[11].data = data.n2opower
  ui[12].data = data.itemslot

  ui[7].up = up.enginepower + love.temp.enginepower
  ui[8].up = up.structure + love.temp.structure
  ui[9].up = up.streamline + love.temp.streamline
  ui[10].up = up.fueltank + love.temp.fueltank
  ui[11].up = up.n2opower + love.temp.n2opower
  ui[12].up = up.itemslot + love.temp.itemslot

  ui[14].obj.text = love.sum + " ￥"
  ui[30].obj.visible = love.index == playerData.slotSelect
  love.ui.backward(ui[30].obj)
}
love.clearUpgrade = function(){
  love.sum = 0
  love.temp = {
    enginepower:0,
    structure:0,
    streamline:0,
    fueltank:0,
    n2opower:0,
    itemslot:0
  }
}
love.upgrade = function(name){
  love.temp[name] += 1
  let data = playerData.slot[love.index]
  if (love.temp[name] + db[data.type][name][0] + data.upgrade[name] >
    db[data.type][name][1]) {
    love.temp[name] -=1
  } else {
    love.sum += love.temp[name] * db[data.type].upgradeprice
  }
  love.setCarData()
}
love.degrade = function(name){
  love.temp[name] -= 1
  let data = playerData.slot[love.index]
  if (love.temp[name] < 0 ) {
    love.temp[name] = 0
  } else {
    love.sum -= (love.temp[name] +1) * db[data.type].upgradeprice
  }
  love.setCarData()
}

love.payment = function(){
  if (playerData.money>= love.sum){
    playerData.money -= love.sum
    for (var name in love.temp) {
      playerData.slot[love.index].upgrade[name] += love.temp[name]
    }
    love.clearUpgrade()
    love.setCarData()
  }
}

love.enter = function () {
  love.graphics.setDesignResolution(1600, 900)
  let font30 = love.loader.data.font30
  love.clearUpgrade()
  var ui = [
    { type: "label", text: "车库-改装厂", x: 100, y: 30, w: 300, h: 100, font: love.loader.data.font50 },
    { type: "label", text: "账户：999999999￥", x: 1120, y: 50, w: 400, h: 100, font: font30 },
    { type: "image", x: 800, y: 200, w: 240 * 3, h: 160 * 3, image: love.loader.data.sheet.extend([0, 0, 24, 16]), fillColor: "white" },
    { type: "label", text: "车型", x: 100, y: 200, w: 150, h: 50, font: font30 },
    { type: "label", text: "未定", x: 300, y: 200, w: 450, h: 50, font: font30 },
    { type: "label", text: "级别", x: 100, y: 270, w: 150, h: 50, font: font30 },
    { type: "label", text: "轿车", x: 300, y: 270, w: 450, h: 50, font: font30 },
    { type: "label", text: "引擎", x: 100, y: 340, w: 150, h: 50, font: font30 },
    { type: "label", text: "强度", x: 100, y: 410, w: 150, h: 50, font: font30 },
    { type: "label", text: "流线", x: 100, y: 480, w: 150, h: 50, font: font30 },
    { type: "label", text: "油箱", x: 100, y: 550, w: 150, h: 50, font: font30 },
    { type: "label", text: "氮氧", x: 100, y: 620, w: 150, h: 50, font: font30 },
    { type: "label", text: "物品", x: 100, y: 690, w: 150, h: 50, font: font30 },
    { type: "label", text: "价格结算", x: 100, y: 760, w: 150, h: 50, font: font30 },
    { type: "label", text: "0$", x: 300, y: 760, w: 450, h: 50, font: font30 },
    { type: "label", text: "+", x: 650, y: 340, w: 50, h: 50, font: font30, onTouch: function () { love.upgrade("enginepower")}},
    { type: "label", text: "+", x: 650, y: 410, w: 50, h: 50, font: font30, onTouch: function () { love.upgrade("structure") }},
    { type: "label", text: "+", x: 650, y: 480, w: 50, h: 50, font: font30, onTouch: function () { love.upgrade("streamline") } },
    { type: "label", text: "+", x: 650, y: 550, w: 50, h: 50, font: font30, onTouch: function () { love.upgrade("fueltank") } },
    { type: "label", text: "+", x: 650, y: 620, w: 50, h: 50, font: font30, onTouch: function () { love.upgrade("n2opower") }},
    { type: "label", text: "+", x: 650, y: 690, w: 50, h: 50, font: font30, onTouch: function () { love.upgrade("itemslot") } },
    { type: "label", text: "-", x: 710, y: 340, w: 50, h: 50, font: font30, onTouch: function () { love.degrade("enginepower") } },
    { type: "label", text: "-", x: 710, y: 410, w: 50, h: 50, font: font30, onTouch: function () { love.degrade("structure") } },
    { type: "label", text: "-", x: 710, y: 480, w: 50, h: 50, font: font30, onTouch: function () { love.degrade("streamline") } },
    { type: "label", text: "-", x: 710, y: 550, w: 50, h: 50, font: font30, onTouch: function () { love.degrade("fueltank") } },
    { type: "label", text: "-", x: 710, y: 620, w: 50, h: 50, font: font30, onTouch: function () { love.degrade("n2opower") } },
    { type: "label", text: "-", x: 710, y: 690, w: 50, h: 50, font: font30, onTouch: function () { love.degrade("itemslot") }  },
    { type: "button", text: "结算", x: 1420, y: 750, w: 100, h: 100, font: font30,onTouch:function(){love.payment()}},
    { type: "button", text: "驾驶", x: 1180, y: 750, w: 100, h: 100, font: font30,
     onTouch() { playerData.slotSelect = love.index; love.setCarData()}},
    { type: "button", text: "返回", x: 1060, y: 750, w: 100, h: 100, font: font30, onTouch() { love.switch("main") } },
    { type: "label",text :"已选中",x : 1320,y : 200,w : 200,h : 50,font:font30}
  ]
  love.ui.load(ui)
  love.uiData = ui
  love.index = 1
  love.setCarData()
}
love.update = function (dt) {

}
love.draw = function () {
  for (var i = 7; i < 13; ++i) {
    love.graphics.setColor("white")
    let d = love.uiData[i]
    love.graphics.setLineWidth(1)
    love.graphics.rectangle("line", d.obj.x + 200, d.obj.y, 320, 50)
    love.graphics.setColor("gray")
    love.graphics.rectangle("fill", d.obj.x + 200, d.obj.y, (d.data[1] / 10) * 320, 50)
    love.graphics.setColor("silver")
    love.graphics.rectangle("fill", d.obj.x + 200, d.obj.y, ((d.data[0] + d.up) / 10) * 320, 50)
  }
  love.graphics.setFont(love.loader.data.font40)
  love.graphics.setColor("black")
  
}
love.touch.onSwipe = function (touch) {
  if (touch.dx < 0) {
    love.index -= 1
    if (love.index < 0) {
      love.index = playerData.slotCount - 1
    }
  } else {
    love.index += 1
    if (love.index > playerData.slotCount - 1) {
      love.index = 0
    }
  }
  love.clearUpgrade()
  for (let i = 3; i < 27; i++) {
    love.timer.newTween(500, love.uiData[i].obj, { x: love.uiData[i].x - 1000 }, "Quadratic.Out",
      function () {
        love.setCarData()
        love.timer.newTween(500, love.uiData[i].obj, { x: love.uiData[i].x }, "Quadratic.Out")
      }
    )
  }
  love.timer.newTween(500, love.uiData[2].obj, { x: love.uiData[2].x + 1000 }, "Quadratic.Out",
    function () {
      love.setCarData()
      love.timer.newTween(500, love.uiData[2].obj, { x: love.uiData[2].x }, "Quadratic.Out")
    }
  )
}

export default love